package com.wimcorp.magic.behaviour.counter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Transient;

import com.wimcorp.magic.behaviour.Behaviour;
import com.wimcorp.magic.cards.Artifact;
import com.wimcorp.magic.cards.Creature;
import com.wimcorp.magic.cards.Enchantment;
import com.wimcorp.magic.cards.Instant;
import com.wimcorp.magic.cards.Land;
import com.wimcorp.magic.cards.Permanent;
import com.wimcorp.magic.cards.Planeswalker;
import com.wimcorp.magic.cards.PlayingCard;
import com.wimcorp.magic.cards.Sorcery;
import com.wimcorp.magic.input.Location;
import com.wimcorp.magic.input.Type;
import com.wimcorp.magic.players.Player;

@Entity
public class CardCountCounter extends Behaviour implements CounterBehavior {

	public CardCountCounter() {
	}

	@Transient
	private Type type;
	@Transient
	private Location location;

	@Transient
	private List<Player> players = new ArrayList<Player>();

	public int getPower() {
		int power = 0;
		for (Player player : players) {
			switch (location) {
			case BATTLEFIELD:
				power += count(type, player.getSummoningPlaines().getExile().getCards());
				break;
			case EXILE:
				power += count(type, player.getSummoningPlaines().getExile().getCards());
				break;
			case GRAVEYARD:
				power += count(type, player.getSummoningPlaines().getGraveyard().getCards());
				break;
			case HAND:
				power += count(type, player.getHand().getCards());
				break;
			}
		}

		return power;
	}

	public int getToughness() {
		int toughness = 0;
		for (Player player : players) {
			switch (location) {
			case BATTLEFIELD:
				toughness += count(type, player.getSummoningPlaines().getExile().getCards());
				break;
			case EXILE:
				toughness += count(type, player.getSummoningPlaines().getExile().getCards());
				break;
			case GRAVEYARD:
				toughness += count(type, player.getSummoningPlaines().getGraveyard().getCards());
				break;
			case HAND:
				toughness += count(type, player.getHand().getCards());
				break;
			}
		}
		return toughness;
	}

	private int count(Type type, Collection<PlayingCard> cards) {
		int counter = 0;

		for (PlayingCard card : cards) {
			if (type.equals(Type.CARD.toString())) {
				counter++;
			} else if (type.equals(Type.ARTIFACT.toString()) && card instanceof Artifact) {
				counter++;
			} else if (type.equals(Type.CREATURE.toString()) && card instanceof Creature) {
				counter++;
			} else if (type.equals(Type.ENCHANTMENT.toString()) && card instanceof Enchantment) {
				counter++;
			} else if (type.equals(Type.INSTANT.toString()) && card instanceof Instant) {
				counter++;
			} else if (type.equals(Type.LAND.toString()) && card instanceof Land) {
				counter++;
			} else if (type.equals(Type.PERMANENT.toString()) && card instanceof Permanent) {
				counter++;
			} else if (type.equals(Type.PLANESWALKER.toString()) && card instanceof Planeswalker) {
				counter++;
			} else if (type.equals(Type.SORCERY.toString()) && card instanceof Sorcery) {
				counter++;
			}

		}
		return counter;
	}

	public List<Player> getPlayers() {
		return players;
	}

	public Type getType() {
		return type;
	}

	public void setType(Type type) {
		this.type = type;
	}

	public Location getLocation() {
		return location;
	}

	public void setLocation(Location location) {
		this.location = location;
	}

	

}
